package PrefixAnd;

public class productExceptSelf {
    public int[] productExceptSelf(int[] nums) {
        int n=nums.length;
        int dp=1;
        int count=0;
        for(int i=0;i<n;i++){
            if(nums[i]!=0){
                dp=nums[i]*dp;
            }
            if(nums[i]==0)count++;
        }
        int[] result=new int[n];
        for(int i=0;i<n;i++){
            if(nums[i]==0){
                if(count==1){
                    result[i]=dp;
                }else{
                    result[i]=0;
                }
            }else{
                if(count>0){
                    result[i]=0;
                }else{
                    result[i]=dp/nums[i];
                }
            }
        }
        return result;
    }
}
